package com.yang.controller;

import com.yang.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {


    @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传:{}, {}, {}", name, age, image);
        //获取原始文件名
        String originalFilename = image.getOriginalFilename();

        //构造唯一的文件名(不能重复) --uuid(通用唯一识别码)ce710304-aaf0-49fd-a9b7-2f06d1f71562
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);
        String newFileName = UUID.randomUUID().toString() + extname;
        log.info("新文件名:{}", newFileName);

        //将文件存储在服务器的磁盘目录中 H:\JavaCodeLearn\JavaProject\springboot-management-system\存储文件
        image.transferTo(new File("H:\\JavaCodeLearn\\JavaProject\\springboot-management-system\\储存文件\\" + newFileName));

        return Result.success();
    }
}
